package com.metal.gear.certification.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.metal.gear.certification.entity.dto.position.SysPositionSaveAndEditDto;
import com.metal.gear.certification.entity.dto.position.SysPositionSearchDto;
import com.metal.gear.certification.entity.po.SysPosition;
import com.metal.gear.certification.service.ISysPositionService;
import com.metal.gear.common.entity.Result;
import com.metal.gear.common.util.query.SearchHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 职务表
 * @Author: jeecg-boot
 * @Date: 2019-09-19
 * @Version: V1.0
 */
@Slf4j
@Tag(name = "3.职务管理")
@RestController
@RequestMapping("/sys/position/l")
public class SysPositionController {
    @Autowired
    private ISysPositionService sysPositionService;

    @Operation(summary = "职务信息分页查询,", description = "用于职务管理列表展示")
    @ApiOperationSupport(order = 1)
    @GetMapping(value = "/a/page",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<IPage<SysPosition>> queryPageList(SysPositionSearchDto searchDto,
                                                    @RequestParam(defaultValue = "1") Integer pageNo,
                                                    @RequestParam(defaultValue = "10") Integer pageSize) throws Exception {
        Page<SysPosition> page = Page.of(pageNo, pageSize);
        QueryWrapper<SysPosition> wrapper = SearchHelper.generalQueryWrapper(searchDto);

        return Result.ok(sysPositionService.page(page, wrapper));
    }

    @Operation(summary = "职务信息查询", description = "查询所有职务信息")
    @ApiOperationSupport(order = 1)
    @GetMapping(value = "/a/list",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<List<SysPosition>> queryPageList(SysPositionSearchDto searchDto) throws Exception {
        QueryWrapper<SysPosition> wrapper = SearchHelper.generalQueryWrapper(searchDto);
        return Result.ok(sysPositionService.list(wrapper));
    }


    @Operation(summary = "新增职务", description = "用于添加一个职务表")
    @ApiOperationSupport(order = 2)
    @PostMapping(value = "/a/add",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<Boolean> add(@RequestBody SysPositionSaveAndEditDto saveAndEditDto) {
        return Result.ok(
                "新增成功!",
                sysPositionService.save(SysPositionSaveAndEditDto.translateTo(saveAndEditDto)));
    }


    @Operation(summary = "编辑职务", description = "对职务进行更新")
    @ApiOperationSupport(order = 3)
    @PutMapping(value = "/a/edit",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<Boolean> edit(@RequestBody SysPosition sysPosition) {
        return Result.ok(
                "修改成功!",
                sysPositionService.updateById(sysPosition));
    }


    @Operation(summary = "删除职务", description = "删除职务")
    @ApiOperationSupport(order = 4)
    @DeleteMapping(value = "/a/delete",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<Boolean> delete(@RequestParam String id) {
        return Result.ok("删除成功!", sysPositionService.removeById(id));
    }


    @Operation(summary = "批量删除职务", description = "批量删除职务")
    @ApiOperationSupport(order = 4)
    @DeleteMapping(value = "/a/deleteBatch",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<Boolean> deleteBatch(@RequestParam String ids) {
        return Result.ok(
                "删除成功!",
                sysPositionService.removeByIds(Arrays.asList(ids.split(","))));
    }


    @Operation(summary = "查询职务详情", description = "查询职务详情")
    @ApiOperationSupport(order = 5)
    @GetMapping(value = "/a/queryById",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<SysPosition> queryById(@RequestParam String id) {
        return Result.ok(sysPositionService.getById(id));
    }


    @Operation(summary = "导出职位数据到ExcelProperty")
    @RequestMapping(value = "/a/exportXls",produces = {MediaType.APPLICATION_JSON_VALUE})
    public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) throws Exception {
        throw new Exception("接口尚未实现");
    }


    @Operation(summary = "导出职位数据模板")
    @RequestMapping(value = "/a/exportXls/template" ,produces = {MediaType.APPLICATION_JSON_VALUE})
    public ModelAndView exportXlsTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        throw new Exception("接口尚未实现");
    }

    @Operation(summary = "导入职位数据")
    @RequestMapping(value = "/a/importExcelProperty", method = RequestMethod.POST ,produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<?> importExcelProperty(@RequestParam("file") MultipartFile file) throws Exception {
        throw new Exception("接口尚未实现");
    }

    @Operation(summary = "职务表-通过code查询", description = "职务表-通过code查询")
    @ApiOperationSupport(order = 6)
    @GetMapping(value = "/a/queryByCode",produces = {MediaType.APPLICATION_JSON_VALUE})
    public Result<SysPosition> queryByCode(@RequestParam String code) {
        return Result.ok(sysPositionService.getByCode(code));
    }

}
